/**
 * 作者:tjp
 * QQ号：57454144
 * QQ群：168895774（有教学视频下载）
 * 官网：http://www.tjpcms.com
 * 码云：https://git.oschina.net/tjpcms/tjpcms
 * 更新日期：2017-01-22
 * tjpcms - 最懂你的cms
 * 2018-05-03：Htgl也就是后台管理
 */
package com.tjpcms.spring.controller;

import com.ndktools.javamd5.Mademd5;
import com.tjpcms.cfg.XT;
import com.tjpcms.common.*;
import com.tjpcms.common.Crud.Crud;
import com.tjpcms.spring.model.Usrpt;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/")
public class AdminLoginController extends TjpcmsController{
	private static final Log logger = LogFactory.getLog(AdminLoginController.class);

	//================================================================================================================================
	//	登录相关开始
	//================================================================================================================================
	//后台登录页面
	@RequestMapping(value = XT.admin_login)
	public String admin_login_page(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HS.setGlobal(request);
		return "admin_login";
	}

	//后台登录页面，这个是只能登录到超级管理员的测试账号
	@RequestMapping(value = XT.dlhtlj_test)
	public String loginhtgl_test(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HS.setGlobal(request);
		return "dlhtlj_test";
	}

	//后台登录页面admin
	@RequestMapping(value = "11223344")
	public String dlhtlj_tjp(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HS.setGlobal(request);
		return "dlhtlj_tjp";
	}

	//后台登录页面admin
	@RequestMapping(value = "dlht_tjp_zy")
	public void dlht_tjp_zy(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.getRequestDispatcher("11223344.dhtml").forward(request, response);//内部跳转过去，这样就不会暴露实际的登录url
	}

	//登录后台tjp
	@RequestMapping(value = "ljdl_ad_tjp", method={RequestMethod.POST})
	public void ljdl_ad_tjp(HttpServletRequest request, HttpServletResponse response) {
		String yhm = request.getParameter("yhm");
		String mm = request.getParameter("mm");

		if (StringUtils.isEmpty(yhm) || StringUtils.isEmpty(mm)){
			HS.flushResponse(response, "用户名或密码错误！");
			return;
		}

		if (!"1122".equalsIgnoreCase(yhm) || !"3344".equalsIgnoreCase(mm)){
			HS.flushResponse(response, "用户名或密码错误！");
			return;
		}

		HT.setCjgly(request, CL.WZMC);
		HS.flushResponse(response, "0");
	}


	//登录后台，这个是直接登录页后台的超级管理员测试账号，就是为了演示系统用的，方便，不用输入，直接登录，还顺带给你看一下后台登录的模样
	@RequestMapping(value = "ljdl_ad_test", method={RequestMethod.POST})
	public void ljdl_ad_test(HttpServletRequest request, HttpServletResponse response) {
		HT.setCjgly(request, "test");
		HS.flushResponse(response, "0");
	}

	//登录后台，这个是要校验用户名密码和验证码的
	@RequestMapping(value = "admin_do_login", method={RequestMethod.POST})
	public void admin_do_login(HttpServletRequest request, HttpServletResponse response) {
		String yhm = request.getParameter("yhm");//用户名
		String mm = request.getParameter("mm");//密码

		if (StringUtils.isEmpty(yhm) || StringUtils.isEmpty(mm)){
			HS.flushResponse(response, "用户名或密码错误！");
			return;
		}

		String yzm = request.getParameter("yzm");
		String yzmSes = (String)request.getSession().getAttribute("idCode");
		if (StringUtils.isEmpty(yzmSes) || StringUtils.isEmpty(yzm) || !yzmSes.equalsIgnoreCase(yzm)){
			HS.flushResponse(response, "验证码错误！");
			return;
		}

		//这里的登录只能登录FP用户，即后台用户管理里新增的那些FP用户，前台注册的用户在前台的弹出登录框里登录
		Mademd5 mad=new Mademd5();
		List<Map<String, Object>> r = _e.r("select id,nc,tx,jsid from tjpcms_usr where left(id,3)='FP_' and mid(id,4)='"+yhm+"' and mm='"+mad.toMd5(mad.toMd5(mm), "FP_"+yhm)+"' ");
		if (CollectionUtils.isEmpty(r) || r.size()!=1){
			HS.flushResponse(response, "用户名或密码错误！");
			return;
		}

		Usrpt usr = new Usrpt();
		usr.setId("FP_"+yhm);
		usr.setNc((String)r.get(0).get("nc"));
		usr.setTx((String)r.get(0).get("tx"));
		usr.setJsid((String)r.get(0).get("jsid").toString());
		HT.setUsr(request, usr);
		HS.flushResponse(response, "0");
	}
	//================================================================================================================================
	//	登录相关结束
	//================================================================================================================================


}